package com.fit.carservice.pages;

import java.util.LinkedList;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.beans.CarBean;
import com.fit.beans.CityBean;
import com.fit.beans.ClientAddressBean;
import com.fit.beans.ClientBean;
import com.fit.enums.CityEnum;
import com.fit.interfaces.CarI;
import com.fit.interfaces.CityI;
import com.fit.interfaces.ClientAddressI;
import com.fit.interfaces.ClientI;
import com.fit.service.CarsI;
import com.fit.service.CitiesI;
import com.fit.service.ClientsAddressIservice;
import com.fit.service.ClientsI;

public class EntryClient {

	@Persist
	@Property
	private ClientI currentClient;
	
	@Persist
	@Property
	private ClientAddressI currentClientAddress;
	
	@Persist
	@Property
	private CityEnum city;
	
	@Persist
	@Property
	private CityI currentCity;
	
	@Persist
	@Property
	private CarI currentCar;
	
	@Inject
	private CarsI cars;
	
	@Inject
	private ClientsI clients;
	
	@Inject
	private ClientsAddressIservice clientsAddress;
	
	@Inject
	private CitiesI cities;
	
	@InjectPage
	private ViewClients viewClientPage;
	
	@SetupRender
	public void createObject(){
		currentClient = new ClientBean();
		currentClientAddress = new ClientAddressBean();
		currentCity = new CityBean();
		currentCar = new CarBean();
		currentCar.setClients(new LinkedList<ClientI>());
		currentClientAddress.setClients(new LinkedList<ClientI>());
		
	}
	
	Object onSuccess(){
		currentCity = cities.retriveSingleCity(city.getValue());
		
		currentClientAddress.executeClientAddressCreation(currentClientAddress.getStreet(), currentCity);
		clientsAddress.save(currentClientAddress);
		cars.save(currentCar);
//		clientAddressBean = 
//			clientsAddress.retriveSingleClientAddress(currentClientAddress.getAddressID());
//		CarI carBean = cars.retriveSingleCar(currentCar.getCarID()); 
		ClientI clientBean = new ClientBean();
			clientBean.executeClientCreation(currentClient.getFirstName(), currentClient.getLastName(),
					currentClient.getPhone(), currentClient.getEmail(), currentClientAddress, currentCar);
			clients.save(clientBean);
		
		

		return viewClientPage;
	}
}
